import { Object3D, Vector3 } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param newScale THREE.Vector3
 * @param optionalOldScale THREE.Vector3
 * @constructor
 */
class SetScaleCommand extends Command {
	object:Object3D;
	oldScale?:Vector3;
	newScale?:Vector3;
	constructor( editor:Editor, object:Object3D, newScale?:Vector3, optionalOldScale?:Vector3 ) {

		super( editor );

		this.type = 'SetScaleCommand';
		this.name = 'Set Scale';
		this.updatable = true;

		this.object = object;

		if ( object !== undefined && newScale !== undefined ) {

			this.oldScale = object.scale.clone();
			this.newScale = newScale.clone();

		}

		if ( optionalOldScale !== undefined ) {

			this.oldScale = optionalOldScale.clone();

		}

	}

	execute() {
		if(this.newScale)
		this.object.scale.copy( this.newScale );
		this.object.updateMatrixWorld( true );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	undo() {
		if(this.oldScale)
		this.object.scale.copy( this.oldScale );
		this.object.updateMatrixWorld( true );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	update( command:SetScaleCommand ) {
		if(command.newScale)
		this.newScale?.copy( command.newScale );

	}

}

export { SetScaleCommand };
